package com.ruoyi.common.utils.pdf;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.ruoyi.common.core.domain.entity.SysOrder;
import com.ruoyi.common.utils.file.FileHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.el.parser.ParseException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
public class PdfUtils {
    private static final String FONT_PATH = "C:\\windows\\Fonts\\simsun.ttc,0";

    public static void pdfPrint(SysOrder sysOrder) throws IOException {
        String inputFilePath = "http://localhost/dev-api" + sysOrder.getProperty().getContract().getFile();
        String outputFilePath = FileHelper.resolveOutputFilePath(sysOrder);
        PdfDocument document = new PdfDocument(new PdfReader(inputFilePath), new PdfWriter(outputFilePath));
        PdfAcroForm form = PdfAcroForm.getAcroForm(document, false);
        PdfFont font = PdfFontFactory.createFont(FONT_PATH);
        sysOrder.setContract(outputFilePath);
        System.out.println("合同开始时间 =======================" + sysOrder.getStartTime());
        DeviceRgb color = new DeviceRgb(0, 0, 0);
        // 创建押金规则数值与描述的映射表
        Map<Integer, String> depositRuleMapping = new HashMap<>();
        depositRuleMapping.put(0, "无押金");
        depositRuleMapping.put(1, "押一付一");
        depositRuleMapping.put(3, "押一付三"); // 对于3显示押一付二


        // 获取押金规则数值
        int depositRuleValue = sysOrder.getProperty().getDepositRule();

        // 将押金规则数值转换为描述，并存入map
        String depositRuleDescription = depositRuleMapping.get(depositRuleValue);
        HashMap<String, String> map = new HashMap<>();
        map.put("homeowner_user_name", sysOrder.getProperty().getHomeowner().getUserName());
        map.put("address", sysOrder.getProperty().getAddress());
        map.put("order_user", sysOrder.getOrderUser());
        map.put("duration", String.valueOf(sysOrder.getDuration()));
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
        Date startDate = null;
        try {
            startDate = sdf.parse(sysOrder.getStartTime().toString());
        } catch (java.text.ParseException e) {
            throw new RuntimeException(e);
        }
        Date endDate = null;
        try {
            endDate = sdf.parse(sysOrder.getEndTime().toString());
        } catch (java.text.ParseException e) {
            throw new RuntimeException(e);
        }

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        int startYear = startCalendar.get(Calendar.YEAR);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        int endYear = endCalendar.get(Calendar.YEAR);

        // 使用startYear和endYear变量
        map.put("startDate_year", String.valueOf(startYear));
        map.put("endDate_year", String.valueOf(endYear));

        map.put("startDate_mounth", String.valueOf(startCalendar.get(Calendar.MONTH) + 1)); // 注意月份是从0开始的，所以加1
        map.put("startDate_day", String.valueOf(startCalendar.get(Calendar.DAY_OF_MONTH)));

        map.put("endDate_mounth", String.valueOf(endCalendar.get(Calendar.MONTH) + 1));
        map.put("endDate_day", String.valueOf(endCalendar.get(Calendar.DAY_OF_MONTH)));

        map.put("total_price_all", String.valueOf(sysOrder.getProperty().getDeposit()));
        map.put("price", String.valueOf(sysOrder.getProperty().getPrice()));
        map.put("depositRule", depositRuleDescription != null ? depositRuleDescription : "未知押金规则");
        map.put("total_price", sysOrder.getTotalPrice().toString());
        map.put("remark", sysOrder.getRemark());
        map.put("homeowner_user_phonenumber", sysOrder.getProperty().getHomeowner().getPhonenumber());
        map.put("order_phone", sysOrder.getOrderPhone());
        System.out.println("============================================================="+sysOrder.getCreatTime());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime now = LocalDateTime.now();
        String formattedCreateTime = now.format(formatter);
        map.put("creat_time", formattedCreateTime);
        for (Map.Entry<String,String> entry : map.entrySet()){
            form.getFormFields().get(entry.getKey()).setValue(entry.getValue()).setColor(color).setFont(font);
        }
        form.flattenFields();

        document.close();


    }
}
